package com.sxm.yiwei.activitis;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MineMoney extends BaseActivity implements View.OnClickListener{
    private ImageView moneyback;
    private Button moneycancel;
    private Button moneyconfirm;
    private WebView web;
    private SharedPreferences sharedPreferences;
    private String shopid;
    private String isInsurance;
    private String type;
    private String token;
    private String userId;
    private String appId;
    private String isOpenedStore="false";
    private String auditStatus;
    private String avatar;
    private String id;
    private String type1;
    private Button cancelbt;
    private Button surebt;
    private AlertDialog dialog;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_money);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);

        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        initMineMoneyView();
        getIsOpenStoreState();
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl(InternetAddressUtis.Url+InternetAddressUtis.XBAO);
        if (isOpenedStore.equals("true")){
            getIsPayServiceMoney();
            getShopMessage();
            moneyconfirm.setText("支付");

            moneycancel.setVisibility(View.VISIBLE);
        }else {
            moneyconfirm.setText("开店");
            moneycancel.setVisibility(View.GONE);
            AlertDialog.Builder builder = new AlertDialog.Builder(MineMoney.this);
            View dialog1 = LayoutInflater.from(MineMoney.this).inflate(R.layout.custom_dislog_1, null);
            builder.setView(dialog1);
            TextView message = (TextView) dialog1.findViewById(R.id.custom_message);
            cancelbt = (Button) dialog1.findViewById(R.id.custom_dialog_cancelbt);
            surebt = (Button) dialog1.findViewById(R.id.custom_dialog_surebt);
            cancelbt.setText("取消");
            surebt.setText("马上开店");
            message.setText("你还未开通属于你自己的店铺 点击马上开店");
            dialog = builder.create();
            cancelbt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            surebt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(MineMoney.this,FWOpenShopActivity.class);
                    intent.putExtra("isOpenedStore", isOpenedStore);
                    startActivity(intent);
                    dialog.dismiss();
                    finish();
                }
            });
            dialog.show();
        }
    }

    // 是否已开店
    private void getIsOpenStoreState() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.ISOPENSTORE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                JSONObject jb = JSON.parseObject(result);
                String status = jb.getString("status");
                if (status.equals("1")){
                    getStoreStateData(result);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(MineMoney.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
        // 是否开店信息
    private void getStoreStateData(String s) {

        JSONObject jb = JSON.parseObject(s);
        String data = jb.getString("data");
        JSONObject object = JSON.parseObject(data);
        isOpenedStore = object.getString("isOpenedStore");

        if (isOpenedStore.equals("true")){
            getIsPayServiceMoney();
            getShopMessage();
            moneyconfirm.setText("支付");

            moneycancel.setVisibility(View.VISIBLE);
        }else {
            moneyconfirm.setText("开店");
            moneycancel.setVisibility(View.GONE);
            AlertDialog.Builder builder = new AlertDialog.Builder(MineMoney.this);
            View dialog1 = LayoutInflater.from(MineMoney.this).inflate(R.layout.custom_dislog_1, null);
            builder.setView(dialog1);
            TextView message = (TextView) dialog1.findViewById(R.id.custom_message);
            cancelbt = (Button) dialog1.findViewById(R.id.custom_dialog_cancelbt);
            surebt = (Button) dialog1.findViewById(R.id.custom_dialog_surebt);
            cancelbt.setText("取消");
            surebt.setText("马上开店");
            message.setText("你还未开通属于你自己的店铺 点击马上开店");
            dialog = builder.create();
            cancelbt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            surebt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(MineMoney.this,FWOpenShopActivity.class);
                    intent.putExtra("isOpenedStore", isOpenedStore);
                    startActivity(intent);
                    dialog.dismiss();
                    finish();
                }
            });
            dialog.show();
        }

    }

    // 获取店铺信息
    private void getShopMessage() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETSTOREMESSAGE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                JSONObject object = JSON.parseObject(result);
                String data = object.getString("data");
                JSONObject object1 = JSON.parseObject(data);
                id = object1.getString("id");
                type1 = object1.getString("type");

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(MineMoney.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                dialog.dismiss();
            }
        });
    }
            // 初始化控件
    private void initMineMoneyView() {
        moneyback = (ImageView) findViewById(R.id.mine_money_back);
        moneycancel = (Button) findViewById(R.id.mine_money_cancel);
        moneyconfirm = (Button) findViewById(R.id.mine_money_confirm);
        web = (WebView) findViewById(R.id.mine_money_web);
        backmain = (ImageView) findViewById(R.id.minemoney_backmain);
        // 设置点击事件监听
        backmain.setOnClickListener(this);
        moneyback.setOnClickListener(this);
        moneycancel.setOnClickListener(this);
        moneyconfirm.setOnClickListener(this);

    }
        // 点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.minemoney_backmain:
                startActivity(new Intent(MineMoney.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.mine_money_back:
                finish();
                break;
            case R.id.mine_money_cancel:
                finish();
                break;
            case R.id.mine_money_confirm:
                String money ;
                if (type1==null){
                    Intent intent = new Intent(MineMoney.this,FWOpenShopActivity.class);
                    intent.putExtra("isOpenedStore", isOpenedStore);
                    startActivity(intent);
                    finish();
                }else {
                    if (type1.equals("1")){
                        money = "1000";
                    }else {
                        money="2000";
                    }
                    Intent in = new Intent(MineMoney.this,BookPayActivity.class);
                    in.putExtra("from","service");
                    in.putExtra("amount",money);
                    in.putExtra("id",id);
                    in.putExtra("alipay",InternetAddressUtis.PAY_SERVICERMONEY);
                    in.putExtra("yivipay",InternetAddressUtis.PAY_YIVISERVICEMONEY);
                    startActivity(in);
                }


                break;
        }

    }
    // 是否缴纳保证金
    private void getIsPayServiceMoney() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.PAYSERVICEMONEY);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                JSONObject object = JSON.parseObject(result);
                String data = object.getString("data");
                JSONObject object1 = JSON.parseObject(data);
                String isInsurance = object1.getString("isInsurance");

                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putString("isInsurance",isInsurance);
                edit.commit();
                if (isInsurance.equals("true")){
                    Toast.makeText(MineMoney.this, "您已缴纳过保证金", Toast.LENGTH_SHORT).show();
                    moneyconfirm.setEnabled(false);
                    moneyconfirm.setText("您已缴纳过保证金!");
                    moneycancel.setVisibility(View.GONE);
                }else {

                    moneyconfirm.setEnabled(true);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(MineMoney.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
